All files / src/components/user TwoFactorSettings.tsx

0% Statements 0/98
0% Branches 0/62
0% Functions 0/16
0% Lines 0/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
'use client';
 
import { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import {
  Shield,
  ShieldCheck,
  ShieldOff,
  Loader2,
  Copy,
  CheckCircle,
  AlertTriangle} from 'lucide-react';
import { apiClient } from '@/services/api';
import { API_ENDPOINTS } from '@/constants/api';
 
interface TwoFactorStatus {
  enabled: boolean;
  has_backup_codes: boolean;
}
 
interface SetupResponse {
  secret: string;
  qr_code: string;
  backup_codes: string[];
}
 
export default function TwoFactorSettings() {
  const { t } = useTranslation();
  const [status, setStatus] = useState<TwoFactorStatus | null>(null);
  const [isLoading, setIsLoading] = useState(true);
  const [isSettingUp, setIsSettingUp] = useState(false);
  const [setupData, setSetupData] = useState<SetupResponse | null>(null);
  const [verificationCode, setVerificationCode] = useState('');
  const [disableCode, setDisableCode] = useState('');
  const [isVerifying, setIsVerifying] = useState(false);
  const [isDisabling, setIsDisabling] = useState(false);
  const [showDisableForm, setShowDisableForm] = useState(false);
  const [showBackupCodes, setShowBackupCodes] = useState(false);
  const [backupCodes, setBackupCodes] = useState<string[]>([]);
  const [error, setError] = useState('');
  const [success, setSuccess] = useState('');
  const [copiedSecret, setCopiedSecret] = useState(false);
 
  useEffect(() => {
    fetchStatus();
  }, []);
 
  const fetchStatus = async () => {
    try {
      const response = await apiClient.get<TwoFactorStatus>(API_ENDPOINTS.AUTH.TWO_FACTOR_STATUS);
      if (response.success && response.data) {
        setStatus(response.data);
      }
    } catch (err) {
      console.error('Failed to fetch 2FA status:', err);
    } finally {
      setIsLoading(false);
    }
  };
 
  const startSetup = async () => {
    setIsSettingUp(true);
    setError('');
    
    try {
      const response = await apiClient.post<SetupResponse>(API_ENDPOINTS.AUTH.TWO_FACTOR_SETUP, {});
      
      if (response.success && response.data) {
        setSetupData(response.data);
      } else {
        throw new Error(response.error?.details || t('profile.twoFactor.errors.setupFailed'));
      }
    } catch (err: any) {
      setError(err.message || t('profile.twoFactor.errors.setupFailed'));
    } finally {
      setIsSettingUp(false);
    }
  };
 
  const verifyAndEnable = async () => {
    if (!verificationCode || verificationCode.length !== 6) {
      setError(t('profile.twoFactor.errors.invalidCode'));
      return;
    }
    
    setIsVerifying(true);
    setError('');
    
    try {
      const response = await apiClient.post<{ success: boolean; message: string }>(
        API_ENDPOINTS.AUTH.TWO_FACTOR_VERIFY,
        { code: verificationCode }
      );
      
      if (response.success && response.data?.success) {
        setSuccess(t('profile.twoFactor.enabled'));
        setBackupCodes(setupData?.backup_codes || []);
        setShowBackupCodes(true);
        setSetupData(null);
        setVerificationCode('');
        fetchStatus();
      } else {
        setError(response.data?.message || t('profile.twoFactor.errors.verifyFailed'));
      }
    } catch (err: any) {
      setError(err.message || t('profile.twoFactor.errors.verifyFailed'));
    } finally {
      setIsVerifying(false);
    }
  };
 
  const disable2FA = async () => {
    if (!disableCode || disableCode.length < 6) {
      setError(t('profile.twoFactor.errors.invalidCode'));
      return;
    }
    
    setIsDisabling(true);
    setError('');
    
    try {
      const response = await apiClient.post<{ success: boolean; message: string }>(
        API_ENDPOINTS.AUTH.TWO_FACTOR_DISABLE,
        { code: disableCode }
      );
      
      if (response.success && response.data?.success) {
        setSuccess(t('profile.twoFactor.disabled'));
        setShowDisableForm(false);
        setDisableCode('');
        fetchStatus();
      } else {
        setError(response.data?.message || t('profile.twoFactor.errors.disableFailed'));
      }
    } catch (err: any) {
      setError(err.message || t('profile.twoFactor.errors.disableFailed'));
    } finally {
      setIsDisabling(false);
    }
  };
 
  const copyToClipboard = (text: string) => {
    navigator.clipboard.writeText(text);
    setCopiedSecret(true);
    setTimeout(() => setCopiedSecret(false), 2000);
  };
 
  if (isLoading) {
    return (
      <Card>
        <CardContent className="flex items-center justify-center py-8">
          <Loader2 className="w-6 h-6 animate-spin text-muted-foreground" />
        </CardContent>
      </Card>
    );
  }
 
  return (
    <Card>
      <CardHeader>
        <CardTitle className="flex items-center gap-2 text-base">
          {status?.enabled ? (
            <ShieldCheck className="h-4 w-4 text-green-500" />
          ) : (
            <Shield className="h-4 w-4" />
          )}
          {t('profile.twoFactor.title')}
        </CardTitle>
      </CardHeader>
      <CardContent className="space-y-4">
        {/* Status description */}
        <p className="text-sm text-muted-foreground">
          {status?.enabled ? t('profile.twoFactor.enabled') : t('profile.twoFactor.description')}
        </p>
 
        {/* Error/Success Messages */}
        {error && (
          <div className="p-3 bg-destructive/10 border border-destructive/30 rounded-md">
            <p className="text-destructive text-sm">{error}</p>
          </div>
        )}
        
        {success && !showBackupCodes && (
          <div className="p-3 bg-green-500/10 border border-green-500/30 rounded-md">
            <p className="text-green-600 dark:text-green-400 text-sm">{success}</p>
          </div>
        )}
 
        {/* Backup Codes Display */}
        {showBackupCodes && backupCodes.length > 0 && (
          <div className="p-4 bg-amber-500/10 border border-amber-500/30 rounded-md">
            <div className="flex items-start gap-3 mb-3">
              <AlertTriangle className="w-5 h-5 text-amber-500 flex-shrink-0 mt-0.5" />
              <div>
                <h4 className="font-semibold text-amber-600 dark:text-amber-400 mb-1">
                  {t('profile.twoFactor.backupCodes.title')}
                </h4>
                <p className="text-sm text-muted-foreground">
                  {t('profile.twoFactor.backupCodes.description')}
                </p>
              </div>
            </div>
            <div className="grid grid-cols-2 gap-2 mt-4">
              {backupCodes.map((code, index) => (
                <div key={index} className="bg-muted px-3 py-2 rounded font-mono text-sm">
                  {code}
                </div>
              ))}
            </div>
            <Button
              variant="outline"
              size="sm"
              className="mt-4"
              onClick={() => {
                copyToClipboard(backupCodes.join('\n'));
                setShowBackupCodes(false);
                setSuccess('');
              }}
            >
              <Copy className="w-4 h-4 mr-2" />
              {t('profile.twoFactor.backupCodes.copy')}
            </Button>
          </div>
        )}
 
        {/* 2FA Enabled State */}
        {status?.enabled && !showDisableForm && (
          <div className="space-y-4">
            <div className="flex items-center gap-2 text-green-600 dark:text-green-400 text-sm">
              <CheckCircle className="w-4 h-4" />
              <span>{t('profile.twoFactor.enabled')}</span>
            </div>
            <Button
              variant="outline"
              onClick={() => setShowDisableForm(true)}
              className="text-destructive hover:text-destructive"
            >
              <ShieldOff className="w-4 h-4 mr-2" />
              {t('profile.twoFactor.disable')}
            </Button>
          </div>
        )}
 
        {/* Disable Form */}
        {showDisableForm && (
          <div className="space-y-4">
            <p className="text-sm text-muted-foreground">
              {t('profile.twoFactor.disableDescription')}
            </p>
            <div className="space-y-2">
              <Label htmlFor="disable-code">{t('profile.twoFactor.code')}</Label>
              <div className="flex gap-3">
                <Input
                  id="disable-code"
                  type="text"
                  value={disableCode}
                  onChange={(e) => setDisableCode(e.target.value.replace(/\D/g, ''))}
                  placeholder={t('profile.twoFactor.placeholder')}
                  maxLength={8}
                />
                <Button onClick={disable2FA} disabled={isDisabling} variant="destructive">
                  {isDisabling ? <Loader2 className="w-4 h-4 animate-spin" /> : t('profile.twoFactor.disable')}
                </Button>
                <Button
                  variant="outline"
                  onClick={() => {
                    setShowDisableForm(false);
                    setDisableCode('');
                    setError('');
                  }}
                >
                  {t('profile.twoFactor.cancel')}
                </Button>
              </div>
            </div>
          </div>
        )}
 
        {/* Setup Flow - Not started */}
        {!status?.enabled && !setupData && (
          <Button onClick={startSetup} disabled={isSettingUp} variant="secondary">
            {isSettingUp ? (
              <>
                <Loader2 className="w-4 h-4 mr-2 animate-spin" />
                {t('profile.twoFactor.verifying')}
              </>
            ) : (
              <>
                <Shield className="w-4 h-4 mr-2" />
                {t('profile.twoFactor.enable')}
              </>
            )}
          </Button>
        )}
 
        {/* QR Code and Verification */}
        {setupData && (
          <div className="space-y-4">
            <div className="text-center">
              <p className="text-sm text-muted-foreground mb-4">
                {t('profile.twoFactor.scanQR')}
              </p>
              <div className="inline-block p-4 bg-white rounded-lg">
                {/* eslint-disable-next-line @next/next/no-img-element */}
                <img src={setupData.qr_code} alt={t('profile.twoFactor.qrAlt')} className="w-40 h-40" />
              </div>
            </div>
 
            {/* Manual Entry */}
            <div className="bg-muted rounded-md p-3">
              <p className="text-xs text-muted-foreground mb-2">
                {t('profile.twoFactor.manualEntry')}
              </p>
              <div className="flex items-center gap-2">
                <code className="flex-1 font-mono text-sm break-all">
                  {setupData.secret}
                </code>
                <Button size="sm" variant="ghost" onClick={() => copyToClipboard(setupData.secret)}>
                  {copiedSecret ? (
                    <CheckCircle className="w-4 h-4 text-green-500" />
                  ) : (
                    <Copy className="w-4 h-4" />
                  )}
                </Button>
              </div>
            </div>
 
            {/* Verification */}
            <div className="space-y-2">
              <Label htmlFor="verify-code">{t('profile.twoFactor.verifyCode')}</Label>
              <div className="flex gap-3">
                <Input
                  id="verify-code"
                  type="text"
                  value={verificationCode}
                  onChange={(e) => setVerificationCode(e.target.value.replace(/\D/g, '').slice(0, 6))}
                  placeholder={t('profile.twoFactor.placeholder')}
                  maxLength={6}
                  className="text-center font-mono text-lg tracking-widest"
                />
                <Button onClick={verifyAndEnable} disabled={isVerifying || verificationCode.length !== 6}>
                  {isVerifying ? <Loader2 className="w-4 h-4 animate-spin" /> : t('profile.twoFactor.verify')}
                </Button>
              </div>
            </div>
 
            <Button
              variant="outline"
              onClick={() => {
                setSetupData(null);
                setVerificationCode('');
                setError('');
              }}
            >
              {t('profile.twoFactor.cancel')}
            </Button>
          </div>
        )}
      </CardContent>
    </Card>
  );
}